package com.ef.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.EContractState;
import com.ef.constant.StatusType;
import com.ef.dao.ContractDao;
import com.ef.domain.Contract;
import com.ef.service.ContractService;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class ContractServiceImpl extends BaseServiceImpl implements
		ContractService {

	private ContractDao contractDao;

	public void delete(Contract entity) {
		entity.setStatus(StatusType.INVALID);
		contractDao.update(entity);
	}

	public PaginationSupport<Contract> findByExample(
			CustomExample<Contract> example, int startIndex, int pageSize) {

		return contractDao.findPageByExample(example, startIndex, pageSize);
	}

	public List<Contract> findDuplicateContract(Long contractId, Long leadsid) {
		DetachedCriteria criterion = DetachedCriteria.forClass(Contract.class);
		criterion.createCriteria("leads").add(Restrictions.eq("id", leadsid));
		criterion
				.add(Restrictions.ne("contractState", EContractState.FINISHED));
		if (contractId != null) {
			criterion.add(Restrictions.ne("id", contractId));
		}
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return contractDao.findAllByCriteria(criterion);
	}

	public List<Contract>  findValidContract(Long leadsid){
		DetachedCriteria criterion = DetachedCriteria.forClass(Contract.class);
		criterion.createCriteria("leads").add(Restrictions.eq("id", leadsid));
 		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return contractDao.findAllByCriteria(criterion);
	}
	public Contract findById(Long id) {

		return contractDao.findById(id);
	}

	public void saveOrUpdate(Contract entity) {
		contractDao.clear();
		contractDao.saveOrUpdate(entity);
	}

	public ContractDao getContractDao() {
		return contractDao;
	}

	public void setContractDao(ContractDao contractDao) {
		this.contractDao = contractDao;
	}

}
